package leetcode.linkedlist;

/**
 * 给定两个非空链表来代表两个非负整数，位数按照逆序方式存储，它们的每个节点只存储单个数字。将这两数相加会返回一个新的链表。
 * <p>
 * 你可以假设除了数字 0 之外，这两个数字都不会以零开头。
 * <p>
 * 示例：
 * <p>
 * 输入：(2 -> 4 -> 3) + (5 -> 6 -> 4)
 * 输出：7 -> 0 -> 8
 * 原因：342 + 465 = 807
 */
public class Solution1 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        boolean isGoHead = false;
        int val = l1.val + l2.val;
        if (val > 9) {
            isGoHead = true;
            val -= 10;
        }
        ListNode result = new ListNode(val);
        ListNode temp1 = l1;
        ListNode temp2 = l2;
        ListNode temp = result;
        while (temp1.next != null && temp2.next != null) {
            temp1 = temp1.next;
            temp2 = temp2.next;
            int tempVal = temp1.val + temp2.val;
            tempVal = isGoHead ? tempVal + 1 : tempVal;
            if (tempVal > 9) {
                isGoHead = true;
                tempVal -= 10;
            } else {
                isGoHead = false;
            }
            ListNode nextNode = new ListNode(tempVal);
            temp.next = nextNode;
            temp = nextNode;
        }

        temp1 = temp1.next;
        temp2 = temp2.next;

        if (!isGoHead) {
            if (temp1 != null) {
                temp.next = temp1;
            } else if (temp2 != null) {
                temp.next = temp2;
            }
        } else {
            while (temp1 != null) {
                ListNode tempNode;
                if (isGoHead) {
                    int tempVal = temp1.val + 1;
                    if (tempVal > 9) {
                        tempVal -= 10;
                        isGoHead = true;
                    } else {
                        isGoHead = false;
                    }
                    tempNode = new ListNode(tempVal);
                }else {
                    tempNode = temp1;
                }
                temp.next = tempNode;
                temp =temp.next;
                temp1 = temp1.next;
            }

            while (temp2 != null) {
                ListNode tempNode;
                if (isGoHead) {
                    int tempVal = temp2.val + 1;
                    if (tempVal > 9) {
                        tempVal -= 10;
                        isGoHead = true;
                    } else {
                        isGoHead = false;
                    }
                    tempNode = new ListNode(tempVal);
                }else {
                    tempNode = temp2;
                }
                temp.next = tempNode;
                temp =temp.next;
                temp2 = temp2.next;
            }
            if(isGoHead) {
                ListNode tempHead = new ListNode(1);
                temp.next = tempHead;
            }
        }

        return result;
    }


    public static void main(String[] args) {
        ListNode l1 = new ListNode(1);
        //ListNode ll1 = new ListNode(4);
        //ListNode lll1 = new ListNode(3);
        ListNode l2 = new ListNode(9);
        ListNode ll2 = new ListNode(9);
        //ListNode lll2 = new ListNode(4);
        //l1.next = ll1;
        //ll1.next = lll1;
        l2.next = ll2;
        //ll2.next = lll2;
        ListNode result = new Solution1().addTwoNumbers(l1, l2);
        ListNode temp = result;
        while (temp != null) {
            System.out.println(temp.val);
            temp = temp.next;
        }
    }
}



